<template>
<view class="container">
	<view class="mine-container" :style="{height: `${windowHeight}px`}">
		<view class="top">
			<view class="mydevice">我的信息</view>
		</view>
		<!--顶部个人信息栏-->
		<view class="header-section">
			<view class="flex padding justify-between">
				<view class="flex align-center">
					<view v-if="!avatar" class="cu-avatar xl round bg-white">
						<view class="iconfont icon-people text-gray icon"></view>
					</view>
					<!-- <image v-if="avatar" @click="handleToAvatar" :src="avatar" class="cu-avatar xl round" mode="widthFix">
          </image> -->
					<view v-if="!name" @click="handleToLogin" class="login-tip">
						点击登录
					</view>
					<view v-if="name" @click="handleToInfo" class="user-info">
						<view class="u_title">
							{{ name }}
						</view>
					</view>
				</view>
				<view @click="handleToInfo" class="flex align-center">
					<text class="my">个人信息</text>
					<view class="iconfont icon-right"></view>
				</view>
			</view>
		</view>

		<!-- 卡片框 -->
		<view class="content-section">
			<view v-if="selectedMode" class="mine-actions" :class="{ 'actions-on':  selectedMode.state }">
				<view class="mode-card">
					<view class="mode-mode">情景模式</view>
					<view class="mode-more" @click="hanldeMore">更多</view>
				</view>
				<view class="mode-card2">
					<image :src="getModeIcon(selectedMode.modePic)" class="mode-image"></image>
					<view class="mode-name2">
						<view class="mode-name">{{selectedMode.modeName}}</view>
						<!-- 滑动按钮 -->
						<view class="swiper-list">
							<view class="uni-list-cell uni-list-cell-pd">
								<view class="custom-switch" :class="{ 'switch-on': selectedMode.state }"
									@click="toggleSwitch(selectedModeIndex)">
									<view class="switch-circle" :class="{ 'circle-on':  selectedMode.state }"></view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="menu-list">
			<view class="list-cell list-cell-arrow" @click="handleToEditInfo">
				<view class="menu-item-box">
					<view class="iconfont icon-user menu-icon"></view>
					<view class="text">编辑资料</view>
				</view>
			</view>
			<view class="list-cell list-cell-arrow" @click="handleHelp">
				<view class="menu-item-box">
					<view class="iconfont icon-help menu-icon"></view>
					<view class="text">常见问题</view>
				</view>
			</view>
			<view class="list-cell list-cell-arrow" @click="handleAbout">
				<view class="menu-item-box">
					<view class="iconfont icon-aixin menu-icon"></view>
					<view class="text">关于我们</view>
				</view>
			</view>
			<view class="list-cell list-cell-arrow" @click="handleToSetting">
				<view class="menu-item-box">
					<view class="iconfont icon-setting menu-icon"></view>
					<view class="text">应用设置</view>
				</view>
			</view>
		</view>


	</view>
</view>
</template>

<script>
	import storage from '@/utils/storage'

	export default {
		data() {
			return {
				name: this.$store.state.user.name,
				version: getApp().globalData.config.appInfo.version,
				modeList: [{
					modeName: '守护者',
					modePic: 'o1',
					state: false
				}, {
					modeName: '情景模式2',
					modePic: 'o2',
					state: false
				}, {
					modeName: '沉浸式睡觉',
					modePic: 'o3',
					state: true
				}, {
					modeName: 'KTV',
					modePic: 'o4',
					state: false
				}]
			}
		},
		computed: {
			avatar() {
				return this.$store.state.user.avatar
			},
			windowHeight() {
				return uni.getSystemInfoSync().windowHeight - 50
			},
			selectedMode() {
				return this.findSelectedMode();
			},
			selectedModeIndex() {
				return this.findSelectedModeIndex();
			}
		},
		methods: {
			handleToInfo() {
				this.$tab.navigateTo('/pages/mine/info/index')
			},
			handleToEditInfo() {
				this.$tab.navigateTo('/pages/mine/info/edit')
			},

			handleToLogin() {
				this.$tab.reLaunch('/pages/login')
			},
			handleToAvatar() {
				this.$tab.navigateTo('/pages/mine/avatar/index')
			},
			handleToSetting() {
				this.$tab.navigateTo('/pages/mine/setting/index')
			},
			hanldeMore() {
				uni.navigateTo({
					url: "/pages/mode/mode"
				})
			},
			handleLogout() {
				this.$modal.confirm('确定注销并退出系统吗？').then(() => {
					this.$store.dispatch('LogOut').then(() => {
						this.$tab.reLaunch('/pages/index')
					})
				})
			},
			toggleSwitch(index) {
				this.modeList[index].state = !this.modeList[index].state;
			},
			getModeIcon(modePic) {
				// 返回房间图标的路径
				switch (modePic) {
					case 'o1':
						return '../../static/images/mode/o1.png';
					case 'o2':
						return '../../static/images/mode/o2.png';
					case 'o3':
						return '../../static/images/mode/o3.png';
					case 'o4':
						return '../../static/images/mode/o4.png';
					default:
						return '';
				}
			},
			findSelectedMode() {
				let mode = this.modeList.find(mode => mode.state);
				return mode ? mode : this.modeList[0];
			},
			findSelectedModeIndex() {
				let index = this.modeList.findIndex(mode => mode.state);
				return index !== -1 ? index : 0;
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f5f6f7;
	}
.content {
		
	}
	.my {
		font-size: 13px;
	}

	.mine-container {
		width: 100%;
		height: 100%;


		.header-section {
			padding: 55px 15px 45px 15px;
			background: linear-gradient(to bottom, #654bff, #a397ff);
			color: white;

			.login-tip {
				font-size: 18px;
				margin-left: 10px;
			}

			.cu-avatar {
				border: 2px solid #eaeaea;

				.icon {
					font-size: 40px;
				}
			}

			.user-info {
				margin-left: 15px;

				.u_title {
					font-size: 23px;
					line-height: 45px;
				}
			}
		}

		.content-section {
			position: relative;
			top: -50px;

			.mine-actions {
				margin: 15px 15px;
				padding: 10px 19px 19px 19px; //上右下左
				border-radius: 8px;
				background: #ffffff;

			}

			.actions-on {
				background: linear-gradient(to left, #ffffff, #ddd8ff);
			}
		}

	}

	.mode-card {
		display: flex;
		align-items: center;
		/* 垂直方向居中 */
		justify-content: space-between;
		/* 子元素在容器两端 */
	}

	.mode-mode {
		font-size: 16px;
		color: #4e6490;
		font-weight: bold;
		float: left;
		margin-left: 10px;
	}

	.mode-more {
		color: #7484df;

	}

	.mode-card2 {
		padding-top: 10px;
		display: flex;
		align-items: center;
		/* 垂直方向居中 */
		justify-content: space-between;
		/* 子元素在容器两端 */
	}

	.mode-image {
		height: 95px;
		width: 95px;
	}

	.mode-name2 {}

	.mode-name {
		padding-bottom: 12px;
		font-size: 22px;
	}

	.mode-switch {}

	.swiper-list {
		padding-left: 4px;
	}

	.custom-switch {
		width: 50px;
		height: 25px;
		background-color: #808080;
		/* 默认灰色 */
		border-radius: 25px;
		position: relative;
		cursor: pointer;
		transition: background-color 0.3s;

	}

	.switch-on {
		background-color: #00b900;
		/* 开启时绿色 */
	}

	.switch-circle {
		width: 23px;
		height: 23px;
		background-color: white;
		border-radius: 50%;
		position: absolute;
		top: 1px;
		left: 1px;
		transition: left 0.3s;

	}

	.circle-on {
		left: 26px;
		/* 圆点移动到右边 */
	}

	.menu-list {
		margin-top: -22px;
		height: 12px;
	}

	.menu-item-box {
		height: 22px;

		.text {
			font-size: 15px;
		}
	}

	.mydevice {
		font-size: 18px;
		color: #4e6490;
		font-weight: bold;
	}

	.top {
		display: flex;
		justify-content: center;
		/* 子元素在容器中居中对齐 */
		align-items: center;
		/* 垂直方向居中 */
		height: 52px;
		background-color: white;
		padding: 4px;
		position: fixed;
		/* 将状态栏固定在最上方 */
		top: 0;
		/* 从顶部开始定位 */
		left: 0;
		/* 从左侧开始定位 */
		width: 100%;
		/* 宽度100%，充满整个屏幕 */
		z-index: 9999;
		/* 确保状态栏位于其他内容上方 */
		border-bottom: 1.5px solid #f0f0f0;
		/* 下方灰色边框线 */
	}
</style>